const request = require('request')
const semver = require('semver')
const chalk = require('chalk')
const packageConfig = require('../package.json')

function done () {
  // Ensure minimum supported node version is used
//   if (!semver.satisfies(process.version, packageConfig.engines.node)) {
//     return console.log(chalk.red(
//       '  You must upgrade node to >=' + packageConfig.engines.node + '.x to use vue-cli'
//     ))
//   }
  request({
    url: 'https://registry.npmjs.org/vue-cli',// TODO: 替换
    timeout: 1000
  }, (err, res, body) => {
    if(err) return 
    if (!err && res.statusCode === 200) {
      const latestVersion = JSON.parse(body)['dist-tags'].latest
      const localVersion = packageConfig.version
      if (semver.lt(localVersion, latestVersion)) {
        console.log(chalk.yellow('A newer version of vue-cli is available.'))
        console.log('  latest:    ' + chalk.green(latestVersion))
        console.log('  installed: ' + chalk.red(localVersion))
      }
    }
    // done()
  })
}

module.exports = done
